package com.adam.masterSpringMVC4.controller;

import com.adam.masterSpringMVC4.weibo.WeiboData;
import com.adam.masterSpringMVC4.weibo.WeiboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

@Controller
public class WeiboController {

//    @Autowired
//    private Twitter twitter;
    @Autowired
    private WeiboService weiboService;

    @RequestMapping("/result")
    public String search(@RequestParam(defaultValue = "Spring") String keyword, Model model) {
        List<WeiboData> weibos = weiboService.search(keyword);
        model.addAttribute("weibos", weibos);
        model.addAttribute("keyword", keyword);
        return "resultPage";
    }

    @RequestMapping(value = "postSearch", method = RequestMethod.POST)
    public String postSearch(HttpServletRequest request, RedirectAttributes redirectAttributes) {
        String keyword = request.getParameter("keyword");
        if(keyword.contains("黄色")) {
            redirectAttributes.addFlashAttribute("error", "违禁关键词");
            return "redirect:/";
        }
        redirectAttributes.addAttribute("keyword", keyword);
        return "redirect:result";
    }

}
